/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package engine.linkList;

/**
 *
 * @author hjp
 */
import java.io.File;
import java.io.Serializable;

//歌单
public class MusicListNode extends AbstractList<MusicNode> implements AbstractNode, Serializable {
    String listName;
//    int sum;
//    Node<MusicNode> head;
//    Node<MusicNode> tail;

    /**
     * 用于创建一个新的歌单
     */
    MusicListNode(String listName){
        this.listName = listName;
        sum = 0;
        head = tail = null;
        isModify = false;
    }

    /**
     * 包内用于创建临时的歌单
     */
    MusicListNode(){
        sum = 0;
        head = tail = null;
        listName = null;
    }

    Node<MusicListNode> sort(SortBy sortBy){
        Node<MusicNode> pOld = head;//遍历排序之前的链表
        Node<MusicNode> newNode;
        Node<MusicNode> pNew;//遍历新构造的链表
        MusicListNode result = new MusicListNode();

        while(pOld != null){
            newNode = new Node<>(pOld.info);
            if (result.head == null){
                result.head = result.tail = newNode;
            }
            else {
                pNew = result.head;
                while(pNew != null){
                    //compare
                    if ((newNode.info.compare(pNew.info,sortBy)) < 0){
                        if (pNew.pre == null) {
                            pNew.addPre(newNode);
                            result.head = newNode;
                        }
                        else{
                             pNew.addPre(newNode);
                        }
                        break;
                    }
                    pNew = pNew.next;
                }
                if (pNew == null){
                    result.tail.addNext(newNode);
                    result.tail = newNode;
                }
            }
            pOld = pOld.next;
        }
        result.sum = sum;
        return new Node<>(result);
    }

//    @Override
//    void add(Node<MusicNode> node){
//        if (head == null){
//            head = tail = node;
//        }
//        else {
//            head.addPre(node);
//            head = node;
//        }
//    }

//    @Override
//    void delete(){
//        listName = null;
//        sum = 0;
//        Node<MusicNode> p = head;
//        Node<MusicNode> q;
//        while(p != null){
//            q = p.next;
//            p.delete();
//            p = q;
//        }
//        head = tail = null;
//    }

    /**
     * 删除整张链表
     */
    @Override
    void delete(){
        listName = null;
        super.delete();
    }


//    /**
//     * 删除歌单中的第index首歌曲
//     */
//    @Override
//    void delete(int index){
//        Node<MusicNode> p = head;
//        if (sum == 1){
//            head = tail = null;
//        }
//        else if (index == 1){
//            head = head.next;
//        }
//        else if (index == sum){
//            p = tail;
//            tail = tail.pre;
//        }
//        else {
//            --index;
//            while (index != 0){
//                p = p.next;
//                --index;
//            }
//        }
//        sum--;
//        isModify = true;
//        p.delete();
//    }

    /**
     * @param key 待查找的关键字
     * @return 匹配结点的链表.当本次查找结束之后，需要调用delete方法删除该链表
     */
    //@Override
    Node<MusicListNode> search(String key){
        MusicListNode result = new MusicListNode();
        Node<MusicNode> p = head;
        Node<MusicNode> newNode;
        while(p != null){
            if (p.info.isMatch(key)){
                newNode = new Node<>(p.info);
//                if (result.head == null){
//                    result.head = result.tail = newNode;
//                }
//                else {
//                    result.tail.addNext(newNode);
//                }
                result.addTail(newNode);
                result.sum++;
            }
            p = p.next;
        }
        return new Node<>(result);
    }

    /**
     * @param groupBy 分类方式
     * @return 返回按照分类方式创建的ListNode的链表。返回之前，对于按照ARTIST以及ALBUM分类的链表将进行排序
     */
    MusicLists classify(ClassifyBy groupBy){
        MusicLists list = new MusicLists();
        Node<MusicListNode> pListNode;
        Node<MusicNode> p = head;
        Node<MusicNode> newNode;
        while(p != null){
            pListNode = list.head;
            newNode = new Node<>(p.info);
            String key = null;
            switch (groupBy){
                case ALBUM:{
                    key = p.info.album;
                    break;
                }
                case ARTIST:{
                    key = p.info.artist;
                    break;
                }
                case PATH:{
                    File file = new File(p.info.filePath);
                    key = file.getParent();
                    break;
                }
            }
            while(pListNode != null){
                if (pListNode.info.listName.equals(key)){
                    //insert
//                    pListNode.info.tail.next = newNode;
//                    newNode.pre = pListNode.info.tail;
                    pListNode.info.tail.addNext(newNode);
                    pListNode.info.tail = newNode;
                    pListNode.info.sum++;
                    break;
                }
                pListNode = pListNode.next;
            }
            if (pListNode == null){
                //create a new MusicListNode and insert into MusicLists
                //and then,insert the newNode into this new MusicListNode
                MusicListNode r = new MusicListNode(key);
                r.head = r.tail = newNode;
                r.sum = 1;
                Node<MusicListNode> newListNode = new Node<>(r);
                //playSchedule.add(newListNode);
                list.addTail(newListNode);
//                if (playSchedule.head == null){
//                    playSchedule.head = playSchedule.tail = newListNode;
//                }
//                else {
//                    playSchedule.tail.next = newListNode;
//                    newListNode.pre = playSchedule.tail;
//                    playSchedule.tail = newListNode;
//                }
            }
            p = p.next;
        }
        return list;
    }


    boolean isMatch(String key){
        char []keyArray1 = listName.toCharArray();
        char []keyArray2 = key.toCharArray();
        int i = 0,j = 0;
        int k = i;
        while(i < listName.length() && j < key.length()){
            if (keyArray1[i] == keyArray2[j]) {
                ++i;
                ++j;
            }
            else if(('a' <= keyArray2[j] && 'z' >= keyArray2[j]) || ('A' <= keyArray2[j] && 'Z' >= keyArray2[j])){
                char ch = keyArray2[j];
                char ch1;
                if (('a' <= keyArray2[j] && 'z' >= keyArray2[j])) {
                    ch1 = (char)(ch - 32);
                }
                else{
                    ch1 = (char)(ch + 32);
                }
                if (ch == keyArray1[i] || ch1 == keyArray1[i]) {
                    ++i;
                    ++j;
                }
                else{
                    i = ++k;
                    j = 1;
                }
            }
            else{
                i = ++k;
                j = 1;
            }
        }
        if (j == key.length() ) {
            return true;
        }
        else{
            return false;
        }
    }

    @Override
    public String toString(){
        return listName;
    }
}
